<?php

namespace Home\Controller;

class ProfessController extends BaseController {

    public function getList() {
        $p = I('p', 0, 'intval');
        $size = I('psize', 15, 'intval');
        if ($p) {
            $page = $p * $size;
        }
        $dogs = M('Dog')->where(['user_id' => $this->user_id, 'is_del' => 0])->getField('dog_id', true);
        $where['p.pro_dog_id'] = array('in', $dogs);

        $family = M('Profess')
                ->table('ot_profess p')
                ->field('p.profess_id,d1.dog_name,d2.dog_name as my_dog_name,p.identity,p.status,p.create_time')
                ->join('ot_dog d1 on p.dog_id = d1.dog_id')
                ->join('ot_dog d2 on p.pro_dog_id = d2.dog_id')
                ->where($where)
                ->limit($page, $size)
                ->order('p.create_time desc')
                ->select();
        if (empty($family)) {
            $family = [];
        }
        $this->success('获取成功', $family);
    }

    public function add() {
        $dog_id = I('dog_id', 0, 'intval');

        if ($dog_id == 0) {
            $this->error('请选择要向谁表白');
        }

        $dog_info = M('Dog')->where(['dog_id' => $dog_id, 'user_id' => $this->user_id])->find();

        if ($is_my_dog) {
            $this->error('不能对自己的狗表白');
        }

        $pro_dog_id = I('pro_dog_id', 0, 'intval');

        if ($pro_dog_id == 0) {
            $this->error('请选择谁表白');
        }

        $is_my_dog = M('Dog')->where(['dog_id' => $pro_dog_id, 'user_id' => $this->user_id])->find();

        if (empty($is_my_dog)) {
            $this->error('不是你的狗');
        }

        $identity = I('identity');

        $is_pro = M('Profess')->where(['dog_id' => $dog_id, 'pro_dog_id' => $pro_dog_id, 'status' => 1])->find();

        if ($is_pro) {
            $this->error('已经确认关系了,不要重复表白');
        }

        $is_pro = M('Profess')->where(['dog_id' => $dog_id, 'pro_dog_id' => $pro_dog_id, 'status' => 0])->find();

        if ($is_pro) {
            $this->error('表白已经发送,请等对方回应');
        }


        if (empty($identity)) {
            $this->error('请输入想成为对方的身份');
        }


        $content = I('content');


        $data['dog_id'] = $dog_id;
        $data['pro_dog_id'] = $pro_dog_id;
        $data['identity'] = $identity;
        $data['content'] = $content;

        $res = M('Profess')->add($data);

        $track['relation_id'] = $res;
        $track['dog_id'] = $dog_id;
        $track['type'] = 3;

        M('Track')->add($track);

        if ($res) {
            $this->success('表白发送成功');
        } else {
            $this->error('表白发送失败');
        }
    }

    function send_post($url, $post_data) {

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/json',
                'content' => $post_data,
                'timeout' => 15 * 60 // 超时时间（单位:s）  
            )
        );

        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        return $result;
    }

    public function getMyList() {
        $p = I('p', 0, 'intval');
        $size = I('psize', 15, 'intval');
        if ($p) {
            $page = $p * $size;
        }
        $dogs = M('Dog')->where(['user_id' => $this->user_id, 'is_del' => 0])->getField('dog_id', true);
        $where['p.dog_id'] = array('in', $dogs);

        $family = M('Profess')
                ->table('ot_profess p')
                ->field('p.profess_id,d1.dog_name as my_dog_name,d2.dog_name as dog_name,p.identity,p.status,p.create_time')
                ->join('ot_dog d1 on p.dog_id = d1.dog_id')
                ->join('ot_dog d2 on p.pro_dog_id = d2.dog_id')
                ->where($where)
                ->limit($page, $size)
                ->order('p.create_time desc')
                ->select();
        if (empty($family)) {
            $family = [];
        }
        $this->success('获取成功', $family);
    }

    public function changeStatus() {
        $profess_id = I('profess_id', 0, 'intval');

        if ($profess_id == 0) {
            $this->error('请传入表白ID');
        }

        $status = I('status', 1, 'intval');

        if ($status == 0) {
            $this->error('请选择要同意还是拒绝');
        }

        $data['status'] = $status;

        $res = M('Profess')->where(['profess_id' => $profess_id])->save($data);

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function detail() {
        $profess_id = I('profess_id', 0, 'intval');

        if ($profess_id == 0) {
            $this->error('请传入表白ID');
        }

        $where['p.profess_id'] = $profess_id;

        $map['d1.user_id'] = $this->user_id;

        $map['d2.user_id'] = $this->user_id;
        $map['_logic'] = 'or';
        $where['_complex'] = $map;

        $family = M('Profess')
                ->table('ot_profess p')
                ->field('p.profess_id,d1.dog_name as my_dog_name,d2.dog_name as dog_name,p.identity,p.content,p.status,p.create_time')
                ->join('ot_dog d1 on p.dog_id = d1.dog_id')
                ->join('ot_dog d2 on p.pro_dog_id = d2.dog_id')
                ->where($where)
                ->find();


        if (empty($family)) {
            $this->error('没有找到表白数据');
        }

        $res['profess_id'] = $family['profess_id'];
        $res['title'] = $family['dog_name'] . '向' . $family['my_dog_name'] . '表白想成为' . $family['my_dog_name'] . '的' . $family['identity'];
        $res['content'] = $family['content'];
        $res['status'] = $family['status'];

        $this->success('获取成功', $res);
    }

    public function del() {
        $profess_id = I('profess_id', 0, 'intval');

        if ($profess_id == 0) {
            $this->error('请传入联名关系ID');
        }

        $where['p.profess_id'] = $profess_id;
        
        $map['d1.user_id'] = $this->user_id;

        $map['d2.user_id'] = $this->user_id;
        $map['_logic'] = 'or';
        $where['_complex'] = $map;

        $family = M('Profess')
                ->table('ot_profess p')
                ->field('p.profess_id,d1.dog_name as my_dog_name,d2.dog_name as dog_name,p.identity,p.content,p.status,p.create_time')
                ->join('ot_dog d1 on p.dog_id = d1.dog_id')
                ->join('ot_dog d2 on p.pro_dog_id = d2.dog_id')
                ->where($where)
                ->find();

        if (empty($family)) {
            $this->error('没有找到联名数据');
        }

        $res = M('Profess')->where(['profess_id' => $profess_id])->delete();

        if ($res) {
            $this->success('解除关系成功');
        } else {
            $this->error('解除关系失败');
        }
    }

}
